import json

from devicetest.api import Asserts
from devicetest.aw.OpenHarmony import CommonOH
from devicetest.core.test_case import TestCase, Step

from aw.common import common_setup, common_teardown
from aw.pom import Setting, DeskTop


class PreTest_RK3568_Basic_Setting_5300(TestCase, Setting):
    # 预置条件：
    # 辅助功能中的单声道音频-开关测试
    # 测试步骤：
    # 1、点击打开开关
    # 2、点击关闭开关
    # 预期结果：
    # 1、开关正常开启
    # 2、开关正常关闭

    def __init__(self, controllers):
        self.TAG = self.__class__.__name__
        TestCase.__init__(self, self.TAG, controllers)

    def setup(self):
        common_setup(self.Phone1)

    def process(self):
        Step('步骤1：进入桌面，点击设置-辅助功能，打开辅助功能中的单声道音频-开关')
        CommonOH.startAbility(self.Phone1, self.ability_name, self.bundle_name)
        CommonOH.wait(self.Phone1, 1)
        CommonOH.swipe(self.Phone1, **DeskTop.swipe_up)
        CommonOH.wait(self.Phone1, 3)
        CommonOH.touchByText(self.Phone1, '辅助功能')

        sounds_switch = json.loads(CommonOH.getWidgetProperties(self.Phone1, condition='type/Toggle', index=2))
        sounds_status0 = sounds_switch.get('checked')
        CommonOH.touchByType(self.Phone1, 'Toggle', index=2)
        Step('预期结果1：开关正常开启')
        sounds_switch = json.loads(CommonOH.getWidgetProperties(self.Phone1, condition='type/Toggle', index=2))
        sounds_status1 = sounds_switch.get('checked')
        Asserts.assert_not_equal(sounds_status0, sounds_status1)

        Step('步骤2：点击关闭开关')
        CommonOH.touchByType(self.Phone1, 'Toggle', index=2)
        sounds_switch = json.loads(CommonOH.getWidgetProperties(self.Phone1, condition='type/Toggle', index=2))
        sounds_status2 = sounds_switch.get('checked')
        Step('预期结果2: 开关正常关闭')
        Asserts.assert_equal(sounds_status2, sounds_status0)

    def teardown(self):
        common_teardown(self.Phone1, self.bundle_name)
